﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using Microsoft.Xna.Framework;

namespace TileMapEditor
{
    public partial class FrmNewMap : Form
    {
        public int MapWidth;
        public int MapHeight;
        public Tileset tileset;
        public string TilesetTextureName;
        public string TileLayerName;
        public bool OkClicked = false;
        XmlDocument input;

        public string TilesetName;

        public FrmNewMap()
        {
            InitializeComponent();
            this.Load += new EventHandler(FrmNewMap_Load);
            btnOpenTileSet.Click += new EventHandler(btnOpenTileSet_Click);
            btnOK.Click += new EventHandler(btnOK_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);
        }

        void FrmNewMap_Load(object sender, EventArgs e)
        {
            btnOK.Enabled = false;
            OkClicked = false;
        }

        private void btnOpenTileSet_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "(Tileset *.tset)|*.tset";
            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;
            openFileDialog.Multiselect = false;
            DialogResult dialogResult = openFileDialog.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                try
                {
                    tileset = new Tileset(openFileDialog.FileName);
                    TilesetName =
                    Path.GetFileNameWithoutExtension(openFileDialog.FileName);
                    TilesetTextureName = tileset.TilesetTextureName;
                    btnOK.Enabled = true;
                }
                catch
                {
                    MessageBox.Show(
                    "Error reading tileset.",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                    tileset = null;
                    btnOK.Enabled = false;
                }
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            btnOK.Enabled = false;
            OkClicked = false;
            this.Close();
        }

        void btnOK_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(tbMapWidth.Text, out MapWidth))
            {
                MessageBox.Show("You must enter a numeric value for Tile Map Width",
                "Tile Map Width Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
                return;
            }
            if (!int.TryParse(tbMapHeight.Text, out MapHeight))
            {
                MessageBox.Show("You must enter a numeric value for Tile Map Height",
                "Tile Map Height Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
                return;
            }
            if (MapWidth < 1)
            {
                MessageBox.Show("Tile Map Width can not be less than 1",
                "Tile Map Width Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
                return;
            }
            if (MapHeight < 1)
            {
                MessageBox.Show("Tile Map Height can not be less than 1",
                "Tile Map Height Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(tbLayerName.Text))
            {
                MessageBox.Show("You must enter a name for the new layer.",
                "Layer Name Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
                return;
            }
            btnOK.Enabled = false;
            OkClicked = true;
            TileLayerName = tbLayerName.Text;
            this.Close();
        }
    }
}
